# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=86box
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.2.1
pkgrel=1
pkgdesc="An emulator for classic IBM PC clones (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://86box.net/'
msys2_repository_url="https://github.com/86box/86box"
msys2_references=(
  'aur: 86box'
  'gentoo: app-emulation/86Box'
)
license=('spdx:GPL-2.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
  "${MINGW_PACKAGE_PREFIX}-fluidsynth"
  "${MINGW_PACKAGE_PREFIX}-freetype"
  "${MINGW_PACKAGE_PREFIX}-openal"
  "${MINGW_PACKAGE_PREFIX}-libpng"
  "${MINGW_PACKAGE_PREFIX}-rtmidi"
  "${MINGW_PACKAGE_PREFIX}-libslirp"
  "${MINGW_PACKAGE_PREFIX}-qt6-base"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-qt6-tools"
  "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
)
source=("https://github.com/${_realname}/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-use-unix-install-layout.patch"
        "0002-fix-unicode-winapi.patch")
sha256sums=('9d5627ef96feb815e66b87f167a1b26f4615fccd730334053293eb038f5ec3a9'
            '9e45dc0600f2f04f87b0c0310a502b489f5de9162d7f7d58ab28dd2c1b35f713'
            '06d168a026455f579774c0b932d54d055ad024618592b4b26bfa8a5f83ade273')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}"/0001-use-unix-install-layout.patch

  # https://github.com/86Box/86Box/pull/5478
  patch -p1 -i "${srcdir}"/0002-fix-unicode-winapi.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MSYSTEM} == CLANGARM64 ]]; then
    # Only the "new" dynarec supports ARM
    extra_config+=("-DNEW_DYNAREC=ON")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DSTATIC_BUILD=OFF \
      -DUSE_QT6=ON \
      "${extra_config[@]}" \
      "../${_realname}-${pkgver}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
